#!/bin/bash
# Copyright (c) 2024 maminjie <canpool@163.com>
# SPDX-License-Identifier: MulanPSL-2.0

CUR_DIR=$(dirname $(readlink -f "$0"))

pkgs_file=$1
specs_dir=$2

result_dir=$CUR_DIR/result
success_file=$result_dir/success
failed_file=$result_dir/failed

if [[ $# -ne 2 || ! -f "$pkgs_file" || ! -d "$specs_dir" ]]; then
    echo "usage: bash $0 pkgs_file specs_dir"
    exit 0
fi

if [ -d "$result_dir" ]; then
    rm -rf "$result_dir"
fi
mkdir -p $result_dir

while read i; do
    if [ -f "$specs_dir/$i" ]; then
        rm -f "$specs_dir/$i"
    fi
    wget https://gitee.com/src-openeuler/$i/raw/master/$i.spec -N -P $specs_dir -o /dev/null
    if [ $? -eq 0 ]; then
        echo "$i" >> "$success_file"
        echo "$i success"
    else
        echo "$i" >> "$failed_file"
        echo "$i failed"
    fi

done < $pkgs_file
